package com.mls.fivedistrict.controller.portal;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.Notify;
import com.mls.fivedistrict.server.NotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 公告管理 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-07-06
 */
@RestController("portalNotifyController")
@RequestMapping("/api/app")
public class NotifyController extends BaseController {
    @Autowired
    NotifyService notifyService;
    @GetMapping("/v1/notify/{notifyId}")
    public ServiceResponse getNotify(@PathVariable Long notifyId) {
        Optional.ofNullable(notifyId).orElseThrow(() -> new CustomerException("公告id不能为空"));
        Notify notify = notifyService.selectById(notifyId);
        if (null != notify) {
            return ServiceResponse.createSuccessResponse(notify);
        }
        return ServiceResponse.createErrorResponse("查无此公告");
    }

    @GetMapping("/v1/notifies")
    public ServiceResponse getNotifyList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(),pageFilter.getPageSize());
        List selectList = notifyService.selectList(pageFilter.getWrapper());
        PageInfo notifyPageInfo = new PageInfo<Notify>(selectList, 5);
        return ServiceResponse.createSuccessResponse(notifyPageInfo);
    }
}

